package com.unsiv.helicopter;

import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioTrack;

/**
 * <p>
 * 音频播放控制对象
 * </p>
 * 
 * @author fanzhen
 * @time 11:23:08 AM Oct 18, 2011
 * @email fanzhen.meng@gmail.com
 */
public class MyAudioHelper {
	private static AudioTrack audioTrackRight;
	private static AudioTrack audioTrackLeft;
	private final static int sampleRate = 44100;// 采样率
	private final static float increment_500 = (float) (2 * Math.PI) * 500
			/ sampleRate; // 500hz
	private final static float increment_1000 = (float) (2 * Math.PI) * 1000
			/ sampleRate; // 1Khz
	private final static float increment_2000 = (float) (2 * Math.PI) * 2000
			/ sampleRate; // 2Khz
	private static int minSize = AudioTrack.getMinBufferSize(sampleRate,
			AudioFormat.CHANNEL_CONFIGURATION_MONO,
			AudioFormat.ENCODING_PCM_16BIT);// 最小周期
	static{
		audioTrackLeft = new AudioTrack(AudioManager.STREAM_MUSIC, sampleRate,
				AudioFormat.CHANNEL_CONFIGURATION_MONO,
				AudioFormat.ENCODING_PCM_16BIT, minSize, AudioTrack.MODE_STREAM);
		audioTrackLeft.setStereoVolume(AudioTrack.getMinVolume(),
				AudioTrack.getMaxVolume());
		audioTrackLeft.play();
		audioTrackRight = new AudioTrack(AudioManager.STREAM_MUSIC, sampleRate,
				AudioFormat.CHANNEL_CONFIGURATION_MONO,
				AudioFormat.ENCODING_PCM_16BIT, minSize, AudioTrack.MODE_STREAM);
		audioTrackRight.setStereoVolume(AudioTrack.getMaxVolume(),
				AudioTrack.getMinVolume());
		audioTrackRight.play();
	}
	private static boolean isCommand = true;
	private final static float duration = 0.02f; // seconds
	private final static int numSamples = (int) (duration * sampleRate);
	private static float[] samples1 = new float[1024];
	
	public static void playCommand() {
//		audioTrackLeft = new AudioTrack(AudioManager.STREAM_MUSIC, sampleRate,
//				AudioFormat.CHANNEL_CONFIGURATION_MONO,
//				AudioFormat.ENCODING_PCM_16BIT, minSize, AudioTrack.MODE_STREAM);
//		audioTrackLeft.setStereoVolume(AudioTrack.getMinVolume(),
//				AudioTrack.getMaxVolume());
//		audioTrackLeft.play();
		new Thread(new Runnable() {
			public void run() {
				double angle = 0;
				while (true) {
					if (!Command.allowCommand)
						break;
					for (int i = 0; i < samples1.length; i++) {
						samples1[i] = (float) Math.sin(angle);
						angle += increment_500;
						if (angle > 2 * Math.PI)
							angle = angle - 2 * Math.PI;
					}
					short[] buffer = new short[1024];
					if (buffer.length < samples1.length)
						buffer = new short[samples1.length];
					for (int i = 0; i < samples1.length; i++)
						buffer[i] = (short) (samples1[i] * Short.MAX_VALUE);
					audioTrackLeft.write(buffer, 0, buffer.length);
				}
			}
		}).start();
//		audioTrackRight = new AudioTrack(AudioManager.STREAM_MUSIC, sampleRate,
//				AudioFormat.CHANNEL_CONFIGURATION_MONO,
//				AudioFormat.ENCODING_PCM_16BIT, minSize, AudioTrack.MODE_STREAM);
//		audioTrackRight.setStereoVolume(AudioTrack.getMaxVolume(),
//				AudioTrack.getMinVolume());
//		audioTrackRight.play();
		new Thread(new Runnable() {
			public void run() {
				while (true) {
					if (!Command.allowCommand)
						break;
					String command = Command.getCommend();
					if (isCommand) {
						isCommand = false;
						audioTrackRight.write(getHeaderSamples(), 0,
								getHeaderSamples().length);
						for (int i = 0; i < command.length(); i++) {
							char c = command.charAt(i);
							if ('0' == c) {
								audioTrackRight.write(get2000HzSamples(), 0,
										get2000HzSamples().length);
							} else {
								audioTrackRight.write(get1000HzSamples(), 0,
										get1000HzSamples().length);
							}
						}
						audioTrackRight.write(get1000HzSamples(), 0,
								get1000HzSamples().length);
					} else {
						isCommand = true;
						audioTrackRight.write(get20msMute(), 0,
								get20msMute().length);
					}
				}
			}
		}).start();
	}

	static void play1() {
		String command = Command.getCommend();
		audioTrackRight.write(getHeaderSamples(), 0, getHeaderSamples().length);
		for (int i = 0; i < command.length(); i++) {
			char c = command.charAt(i);
			if ('0' == c) {
				audioTrackRight.write(get2000HzSamples(), 0,
						get2000HzSamples().length);
			} else {
				audioTrackRight.write(get1000HzSamples(), 0,
						get1000HzSamples().length);
			}
		}
		audioTrackRight.write(get1000HzSamples(), 0, get1000HzSamples().length);
	}

	static void play2() {
		double angle = 0;
		for (int i = 0; i < samples1.length; i++) {
			samples1[i] = (float) Math.sin(angle);
			angle += increment_500;
			if (angle > 2 * Math.PI)
				angle = angle - 2 * Math.PI;
		}
		short[] buffer = new short[1024];
		if (buffer.length < samples1.length)
			buffer = new short[samples1.length];
		for (int i = 0; i < samples1.length; i++)
			buffer[i] = (short) (samples1[i] * Short.MAX_VALUE);
		audioTrackLeft.write(buffer, 0, buffer.length);
	}

	// 同步位
	static short[] getHeaderSamples() {
		float smaple[] = new float[(int) (sampleRate / 500 / 2 * 3)];
		float angle = 0;
		for (int i = 0; i < smaple.length; i++) {
			smaple[i] = -(float) Math.sin(angle);
			angle += increment_500;
		}
		short[] buffer = new short[smaple.length];
		if (buffer.length < smaple.length)
			buffer = new short[smaple.length];
		for (int i = 0; i < smaple.length; i++)
			buffer[i] = (short) (smaple[i] * Short.MAX_VALUE);
		return buffer;
	}

	// 1000hz一个周期采样
	static short[] get1000HzSamples() {
		float smaple[] = new float[(int) (sampleRate / 1000)];
		float angle = 0;
		for (int i = 0; i < smaple.length; i++) {
			smaple[i] = (float) Math.sin(angle);
			angle += increment_1000;
		}
		short[] buffer = new short[smaple.length];
		if (buffer.length < smaple.length)
			buffer = new short[smaple.length];
		for (int i = 0; i < smaple.length; i++)
			buffer[i] = (short) (smaple[i] * Short.MAX_VALUE);
		return buffer;
	}

	// 2000hz一个周期采样
	static short[] get2000HzSamples() {
		float smaple[] = new float[(int) (sampleRate / 2000)];
		float angle = 0;
		for (int i = 0; i < smaple.length; i++) {
			smaple[i] = (float) Math.sin(angle);
			angle += increment_2000;
		}
		short[] buffer = new short[smaple.length];
		if (buffer.length < smaple.length)
			buffer = new short[smaple.length];
		for (int i = 0; i < smaple.length; i++)
			buffer[i] = (short) (smaple[i] * Short.MAX_VALUE);
		return buffer;
	}

	// 20ms静音
	static short[] get20msMute() {
		short[] buffer = new short[numSamples];
		for (int i = 0; i < buffer.length; i++)
			buffer[i] = 0;
		return buffer;
	}
}
